package model.handlers;

import database.Database;
import java.sql.SQLException;
import model.Address;
import model.ZipCode;

/**
 *
 * @author Sonny Sandberg
 */
public abstract class AddressHandler 
{
    public static Address getExistingAddress(Address address) throws SQLException, ClassNotFoundException
    {
        Database db = new Database();
        
        String[][] results = db.getFromDB("bs_address", "WHERE street = '"+address.getStreet()+
                "' AND zip_code = '"+address.getZipCode().getZipCode()+"'", "", "", "id", "street", "zip_code");
        db.closeDB();
        
        if (results.length > 0)
        {
            address.setId(Integer.parseInt(results[0][0]));
            address.setStreet(results[0][1]);
            address.setZipCode(getCity(results[0][2]));
        }
        
        return address;
    }
    
    public static Address getExistingAddressByID(int id) throws SQLException, ClassNotFoundException
    {
        Address address = new Address();
        Database db = new Database();
        
        String[][] results = db.getFromDB("bs_address", "WHERE id = '"+id+
                "'", "", "", "id", "street", "zip_code");
        db.closeDB();
        
        if (results.length > 0)
        {
            address.setId(id);
            address.setStreet(results[0][1]);
            address.setZipCode(getCity(results[0][2]));
        }
        
        return address;
    }
    
    public static ZipCode getCity(String zipcode) throws SQLException, ClassNotFoundException
    {
        ZipCode zipObj = null;
        
        Database db = new Database();
        
        String[][] result = db.getFromDB("bs_zip_code", "WHERE zip_code = '"+zipcode+"'", "", "", "zip_code", "city");
        db.closeDB();
        
        if (result.length > 0)
        {
            zipObj = new ZipCode(result[0][0], result[0][1]);
        }
        
        return zipObj;
    }
}